package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



public class products_List extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public products_List() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	//根据条件返回商品信息
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out = response.getWriter();
		JSONArray jsonarray = new JSONArray();
		JSONObject jsonobj = new JSONObject();


		conn cn = new conn();
	
	    String f=request.getParameter("f");

		ResultSet rs=null;

		if(f.equals("1"))
		{

			rs=cn.query("select * from  products where pflag='上架' order by pid desc limit 0,10");
		}
		else if(f.equals("2"))
		{
			String where = " where pflag='上架' ";
			String t = request.getParameter("t");
			String k = request.getParameter("k");

			if (!t.equals("0")) {
				where+=" and a.tid="+t;
			}

			if (!k.equals("")) {
				where+=" and ptitle like '%"+k+"%' ";
			}

			rs=cn.query("select * from  products a "+where+" order by pid desc ");
		}
	 	else 	if(f.equals("3"))
		{
			String id = request.getParameter("id");

			rs=cn.query("select a.*,ifnull(tt,0) as tt from  products a left join" +
					" (select pid,cast(avg(pj*1.0) as decimal(16,1) ) as tt from ordermore group by pid) b on a.pid=b.pid where a.pid="+id);
		}
		while (true)
		{
			try {
				if (!rs.next()) break;
             jsonobj.put("pid", rs.getString("pid"));
             jsonobj.put("tid", rs.getString("tid"));
             jsonobj.put("ptitle", rs.getString("ptitle"));
             jsonobj.put("photo", rs.getString("photo"));
             jsonobj.put("price", rs.getString("price"));
             jsonobj.put("pememo", rs.getString("pememo").replaceAll("/flowershopServer","http://localhost:8080/flowershopServer").replaceAll("<img","<img style=='width:100%' "));
             jsonobj.put("pflag", rs.getString("pflag"));

				if (f.equals("3")) {

					jsonobj.put("tt", rs.getString("tt"));

					ResultSet rs3 = cn.query("select * from ordermore " +
							" where pid=" + rs.getString("pid") + " and pj>0 " +
							" order by ptime desc");
					JSONArray jsonArray4 = new JSONArray();
					JSONObject jsonobj4 = new JSONObject();
					while (rs3.next()) {

						jsonobj4.put("pj", rs3.getString("pj"));
						jsonobj4.put("pjnr", rs3.getString("pjnr"));
						jsonobj4.put("ptime", rs3.getString("ptime"));

						jsonArray4.add(jsonobj4);
					}


					jsonobj.element("ping", jsonArray4);

				}
		
				jsonarray.add(jsonobj);

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		out.println(jsonarray);	
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

